package com.scau.data.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scau.data.entity.OnSellDO;
import com.scau.database.entity.shopowner.ShopOwnerQuery;
import com.scau.database.entity.shopowner.ShopOwnerVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface OnsellMapper extends BaseMapper<OnSellDO> {

    /**
     * 通过商店id、商品名，获取本店在售相关商品的商品名、原价特价是否特价、剩余数量、商品条形码
     * @param query 商店id、商品名
     * @return 商品名、价格、剩余数量、商品条形码
     */
//    @Select("SELECT on_sell.bar_code, on_sell.original_price, " +
//            "on_sell.special_price, on_sell.use_special_price, " +
//            "on_sell.surplus, order_line.commodity " +
//            "FROM on_sell INNER JOIN shelf_ok " +
//            "ON on_sell.bar_code = shelf_ok.bar_code " +
//            "WHERE on_sell.store_id = #{storeId} AND shelf_ok.commodity = #{commodity}")
    @Select("SELECT on_sell.bar_code, on_sell.original_price, " +
            "on_sell.special_price, on_sell.use_special_price, " +
            "on_sell.surplus, commodity_info.`name`  commodity " +
            "from \n" +
            "commodity_info \n" +
            "JOIN on_sell USING(bar_code)\n" +
            "JOIN shelf_ok USING(bar_code)\n" +
            "WHERE commodity_info.`name` = #{commodity}\n" +
            "and on_sell.store_id = #{storeId}\n" +
            "GROUP BY bar_code")
    List<ShopOwnerVO> searchGoodsByStoreIdAndCommodity(ShopOwnerQuery query);

    @Update("update on_sell set surplus = surplus - ${reduce} " +
            "where store_id = #{storeId} " +
            "and bar_code = #{barCode}")
    Integer reduceSurplus(@Param("storeId") Integer storeId,
                          @Param("barCode") String barCode,
                          @Param("reduce") Integer reduce);
    @Update("update on_sell set surplus = surplus + ${add} " +
            "where store_id = #{storeId} " +
            "and bar_code = #{barCode}")
    Integer addSurplus(@Param("storeId") Integer storeId,
                       @Param("barCode") String barCode,
                       @Param("add") Integer add);
    @Update("update on_sell set `count` = `count` + ${add} " +
            "where store_id = #{storeId} " +
            "and bar_code = #{barCode}")
    Integer addCount(@Param("storeId") Integer storeId,
                       @Param("barCode") String barCode,
                       @Param("add") Integer add);


}
